//
//  LZManageReviewsVideosTableView.m
//  laziz_Merchant
//
//  Created by xbn_iMac on 2016/12/18.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZManageReviewsVideosTableView.h"
#import "LZManageReviewsVideosTableViewCell.h"
#import "LZManageReviewsPromotionVideoListModel.h"

@interface LZManageReviewsVideosTableView ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray *dataArr;

@property (nonatomic, assign) int page;
@property (nonatomic, assign) int limit;

@property (nonatomic, assign) BOOL isRequesting;

@end

@implementation LZManageReviewsVideosTableView

-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    self = [super initWithFrame:frame style:style];
    if (self) {
        
        self.separatorColor = [UIColor clearColor];
        self.backgroundColor = UIColorFromRGB(0xeeeeee, 1);
        
        [self registerCell];
        self.dataArr = [NSMutableArray array];
        self.delegate = self;
        self.dataSource = self;
        
        [self addFooterRefresh];
    }
    return self;
}

#pragma mark - Public Method

- (void)checkHaveData {
    if (self.dataArr.count == 0) {
        if (self.subTableViewDelegate && [self.subTableViewDelegate respondsToSelector:@selector(requestDateWithTag:andPrameter:andPage:andLimit:resultSuccess:)]) {
            [self downLoadNewData];
        }
    }
}

/* --------------------- configure Super Refresh method --------------------- */

- (void)downLoadNewData {
    self.page = 1;
    self.limit = 10;
    [self requestListWithIsDownLoad:YES];
}

- (void)upLoadMoreData {
    [self requestListWithIsDownLoad:NO];
}

- (void)requestListWithIsDownLoad:(BOOL)isDownLoad {
    
    if (self.isRequesting) {
        return;
    }
    
    self.isRequesting = YES;
    
    WS(weakSelf)
    
    [self.subTableViewDelegate requestDateWithTag:1 andPrameter:nil andPage:self.page andLimit:self.limit resultSuccess:^(NSArray *result,id otherObject) {
        weakSelf.isRequesting = NO;
        [weakSelf.mj_footer endRefreshing];
        
        if (result) {
            weakSelf.page ++;
            
            if (isDownLoad) {
                [weakSelf.dataArr removeAllObjects];
            }
            
            if (result.count>0) {
                [weakSelf.dataArr addObjectsFromArray:result];
                [weakSelf reloadData];
            }
            else {
                [weakSelf.mj_footer endRefreshingWithNoMoreData];
            }
        }
    }];
}

#pragma mark - Private Method

- (void)registerCell {
    
    [self registerNib:[UINib nibWithNibName:@"LZManageReviewsVideosTableViewCell" bundle:nil] forCellReuseIdentifier:@"LZManageReviewsVideosTableViewCell"];
    
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LZManageReviewsVideosTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LZManageReviewsVideosTableViewCell"];
    cell.model = self.dataArr[indexPath.row];
    return cell;
}


#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 85;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (self.subTableViewDelegate && [self.subTableViewDelegate respondsToSelector:@selector(tableViewTag:didSelectRowAtIndexPath:object:)]) {
        LZManageReviewsPromotionVideoListModel *model = self.dataArr[indexPath.row];
        [self.subTableViewDelegate tableViewTag:1 didSelectRowAtIndexPath:indexPath object:model];
    }
}

@end
